package com.wx.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wx.common.HttpClientUtil;
import com.wx.common.JsonArrayResult;
import com.wx.entity.Address;
import com.wx.service.CommonService;

@RestController
@RequestMapping("/api")
public class CommonController {
	
	@Autowired
	private CommonService commonService;
	
	@GetMapping("/common/shipping-address/province")
	@ResponseBody
	public JsonArrayResult address_province() {
		List<Address> list = commonService.selectAllByLevel(1);
		JsonArrayResult result = new JsonArrayResult(JSONArray.parseArray(JSON.toJSONString(list)));
		return result;
	}
	
	@GetMapping("/common/shipping-address/child")
	@ResponseBody
	public JsonArrayResult address_child(@RequestParam(value = "pid") String pid) {
		List<Address> list = commonService.selectAllByPid(pid);
		JsonArrayResult result = new JsonArrayResult(JSONArray.parseArray(JSON.toJSONString(list)));
		return result;
	}
	
	/**
	 * 获取一级地址菜单
	 */
	@GetMapping("/user/shipping-address/insert_level")
	@ResponseBody
	public void address_insert() {
		String requestUrl = "https://api.it120.cc/common/region/v2/province";
		JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet(requestUrl));
		List<Address> list = JSON.parseArray(jsonObject.getString("data"), Address.class);
		for(Address item : list) {
			commonService.insert(item);
		}
	}
	
	/**
	 * 二级地址菜单
	 */
	@GetMapping("/user/shipping-address/insert_level1")
	@ResponseBody
	public void address_insert1() {
		String requestUrl = "https://api.it120.cc/common/region/v2/child";
		Map<String, String> requestUrlParam = new HashMap<>();
		
		List<Address> list = commonService.selectAllByLevel(1);
		
		if(list.size() != 0) {
			for(Address item : list) {
				requestUrlParam.put("pid", item.getId());
				JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet(requestUrl, requestUrlParam));
				List<Address> child_list = JSON.parseArray(jsonObject.getString("data"), Address.class);
				
				if(child_list == null) {
					requestUrlParam.clear();
					continue;
				}
				
				for(Address child_item : child_list) {
					commonService.insert(child_item);
				}
				requestUrlParam.clear();
			}
		}
	}
	
	/**
	 * 三级地址菜单
	 */
	@GetMapping("/user/shipping-address/insert_level2")
	@ResponseBody
	public void address_insert2() {
		String requestUrl = "https://api.it120.cc/common/region/v2/child";
		Map<String, String> requestUrlParam = new HashMap<>();
		
		List<Address> list = commonService.selectAllByLevel(2);
		
		if(list.size() != 0) {
			for(Address item : list) {
				requestUrlParam.put("pid", item.getId());
				JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet(requestUrl, requestUrlParam));
				List<Address> child_list = JSON.parseArray(jsonObject.getString("data"), Address.class);
				
				if(child_list == null) {
					requestUrlParam.clear();
					continue;
				}
				
				for(Address child_item : child_list) {
					commonService.insert(child_item);
				}
				requestUrlParam.clear();
			}
		}
	}

}
